import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:saas_order_meal/res/resources.dart';
import 'package:saas_order_meal/saas_order_meal.dart';

import '../net/apis.dart';
import '../net/net_response/order_detail_data.dart';
import '../viewmodel/has_ordered_detail_view_model.dart';

class HasOrderDetailContent extends BaseStatefulMvvmWidget{
  final  OrderDetailData detail;

  HasOrderDetailContent({required this.detail});

  @override
  State<StatefulWidget> createState() => _HasOrderDetailState();

}

class _HasOrderDetailState extends BaseMvvmState<HasOrderedDetailViewModel,HasOrderDetailContent>{
  @override
  Widget? buildLoadingContentView() {
    var detail = widget.detail;
    return Column(
      children: [
        SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 16,vertical: 16),
          child:
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CommonText('已下单菜品'),
                SizedBox(height: 12,),
                Divider(),
                SizedBox(height: 12,),
                ...(detail.goodsList??[]).map((e) {
                  return Row(
                    children: [
                      CachedNetworkImage(
                        imageUrl: "${Apis.imageHead}${e.imgAttachId}",
                        width: 60,
                        height: 60,
                        httpHeaders: ImageLoad.globalHeader,
                        placeholder: (context, url) {
                          return Image.asset(
                            "images/none.png",
                            package: "saas_order_meal",
                            width: 60,
                            height: 60,
                          );
                        },
                        errorWidget: (context, url, error) {
                          return Image.asset(
                            "images/none.png",
                            package: "saas_order_meal",
                            width: 60,
                            height: 60,
                          );
                        },
                      ),
                      // ImageLoad(
                      //   "${Apis.imageHead}${e.imgAttachId}",
                      //   fit: BoxFit.cover,
                      //   width: 60,
                      //   height: 60,
                      //   placeholder: "images/none.png",
                      //   errorImage: "images/none.png",
                      //   package: "saas_order_meal",
                      // ),
                      SizedBox(width: 12,),
                      
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Row(
                            children: [
                              CommonText(e.name).setWeight(1),
                              CommonText("¥${e.price}")
                            ],
                          ),
                          CommonText("x${e.number}")
                        ],
                      ).setWeight(1)
                    ],
                  ).addToContainer(margin: EdgeInsets.only(bottom: 12),height: 60);
                }).toList(),
                Divider(),
                SizedBox(height: 12,),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Column(
                      children: [
                        CommonText('合计：${detail.totalMoney}'),
                        SizedBox(height: 12,),
                        CommonText('实付：${detail.actualMoney}'),
                      ],
                    )
                  ],
                )
              ],
            ).addToContainer(
              padding: EdgeInsets.symmetric(horizontal: 12,vertical: 16),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: 10.borderRadius
              )
            ),
        ).setWeight(1),

        if(SaasOrderMeal.cancelFood)
        TextButton(onPressed: (){
          viewModel.cancelClick();
        }, child: CommonText('取消订单',textColor: Colors.white),
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(SaasOrderMeal.mainColor,),
          shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: 0.borderRadius)),
          minimumSize: MaterialStateProperty.all(Size(double.infinity,50))
        ),)
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return loadingContentWidget;
  }

  @override
  HasOrderedDetailViewModel createViewModel() {
    return HasOrderedDetailViewModel(context,detail: widget.detail);
  }

  @override
  void onRetryClick() {
  }

}